<?php
namespace Admin\Controller;
use Think\Controller;
class NewsController extends BaseController {
    public function index(){
        $news=M('news');
        $newslist=$news->select();
        $data=$_GET['type'];
        if(empty($data)){
            $data=1;
        }
        $newslist=$news->where("type=".$data)->select();
        // var_dump($joinuslist);
        $this->assign("type",$_GET['type']);
        $this->assign("newslist",$newslist);
    	$this->login_verify();
        $this->display();
    }
    
       public function getChange(){
        $data['type']=$_POST['type'];
        $news=M("news");
        $newslist=$news->where("type=".$data['type'])->select();
        $this->ajaxReturn($newslist);
    }

     public function addNews(){
    	// var_dump($_POST);
    	$class=M('news');
        $data['type']=$_POST['type'];
    	$data['title']=$_POST['title'];
    	$data['author']=$_POST['author'];
    	$data['content']=$_POST['content'];
   		$data['time']=date('Y-m-d H:i:s');
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     2097152 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->saveName = 'time';
        $upload->subName = 'news';
        $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
        // 上传单个文件 
        $info   =   $upload->uploadOne($_FILES['news']);
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功 获取上传文件信息
            $data['imgurl']='Uploads/'.$info['savepath'].$info['savename'];
    	if($class->add($data)){
    		$this->success('添加成功', U('News/index'));
    	}else{
    		$this->error('添加失败');
    	}
    }
}    
        // 删除操作
        public function newsDel(){
        $id=I('get.id');
        $data['type']=$_POST['type'];
        $news=M('news');
        if($news->where('id='.$id)->delete()){
                $this->success("删除成功",U('News/index'));
        }else{
                $this->error("删除失败");
        }
    
    }
}    